package com.um.jdy.ykc.command;

import com.um.jdy.ykc.utils.RadixsUtil;
import lombok.Data;

/**
 * 充电过程 BMS 需求、充电机输出（充电桩->运营平台）
 *
 * @author 甘天伟
 * @since 2022/6/28
 * 版权所有 广州优匠科技有限公司
 */
@Data
public class Cmd23BmsRequireWhenCharging extends YkcCommand {

    /**
     * 交易流水号：16个字节
     */
    private String transactionNo;

    /**
     * 桩编码：7个字节
     */
    private String code;

    /**
     * 枪号：1个字节
     */
    private int gunNo;

    /**
     * BMS 电压需求：2个字节
     */
    private int bmsRequireVoltage;

    /**
     * BMS 电流需求：2个字节
     */
    private int bmsRequireElectricity;

    /**
     * BMS 充电模式：1个字节
     */
    private int bmsChargeMode;

    /**
     * BMS 充电电压测量值：2个字节
     */
    private int bmsChargeVoltage;

    /**
     * BMS 充电电流测量值：2个字节
     */
    private int bmsChargeElectricity;

    /**
     * BMS 最高单体动力蓄电池组号：4位
     */
    private int bmsSingleBatteryMaxVoltageNo;

    /**
     * BMS 最高单体动力蓄电池电压及组号：2个字节
     */
    private int bmsSingleBatteryMaxVoltage;

    /**
     * BMS 当前荷电状态 SOC：1个字节
     */
    private int bmsSoc;

    /**
     * BMS 充电电流测量值：2个字节
     */
    private int bmsPredictRemainMinutes;

    /**
     * 电桩电压输出值：2个字节
     */
    private int outputVoltage;

    /**
     * 电桩电流输出值：2个字节
     */
    private int outputElectricity;

    /**
     * 累计充电时间：2个字节
     */
    private int totalChargeMinutes;

    @Override
    public void parseBytes(byte[] bytes) {
        super.parseBytes(bytes);
        transactionNo = RadixsUtil.bytesToHexString(readBytes(data, 0, 16));
        code = RadixsUtil.bytesToHexString(readBytes(data, 16, 7));
        gunNo = RadixsUtil.byteToInteger(data[23]);
        bmsRequireVoltage = RadixsUtil.bytesToInteger(readBytes(data, 24, 2), true);
        bmsRequireElectricity = RadixsUtil.bytesToInteger(readBytes(data, 26, 2), true);
        bmsChargeMode = RadixsUtil.byteToInteger(data[28]);
        bmsChargeVoltage = RadixsUtil.bytesToInteger(readBytes(data, 29, 2), true);
        bmsChargeElectricity = RadixsUtil.bytesToInteger(readBytes(data, 31, 2), true);

        String maxVolBinary = RadixsUtil.bytesToBinary(readBytes(data, 33, 2, true));
        bmsSingleBatteryMaxVoltageNo = RadixsUtil.binaryToInteger(maxVolBinary.substring(0, 4)); // 前4位为编号
        bmsSingleBatteryMaxVoltage = RadixsUtil.binaryToInteger(maxVolBinary.substring(4, 4 + 12)); // 后12位为电压

        bmsSoc = RadixsUtil.byteToInteger(data[35]);
        bmsPredictRemainMinutes = RadixsUtil.bytesToInteger(readBytes(data, 36, 2), true);
        outputVoltage = RadixsUtil.bytesToInteger(readBytes(data, 38, 2), true);
        outputElectricity = RadixsUtil.bytesToInteger(readBytes(data, 40, 2), true);
        totalChargeMinutes = RadixsUtil.bytesToInteger(readBytes(data, 42, 2), true);

        // 处理电流偏移量
        bmsRequireElectricity = parseCurrentOffset(bmsRequireElectricity);
        bmsChargeElectricity = parseCurrentOffset(bmsChargeElectricity);
        outputElectricity = parseCurrentOffset(outputElectricity);

        transactionNo = RadixsUtil.getTransactionNo(transactionNo);
        code = RadixsUtil.getCode(code);
    }

    /**
     * 解析电流偏移量
     * @param current
     * @return
     */
    private int parseCurrentOffset(int current){
        return (int) Math.abs(((current / 10d) - 400) * 10);
    }
}
